package com.bawei.demo_shoppingcar.activity;

import android.content.Context;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Display;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;

import com.bawei.demo_shoppingcar.R;

import java.io.IOException;

public class PhotoActivity extends AppCompatActivity {

    private SurfaceView surfaceView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Window window = getWindow();//得到窗口
        requestWindowFeature(Window.FEATURE_NO_TITLE);//没有标题
        window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置全屏
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);//设置高亮
        /**
         * 先要设置显示方式  在显示布局控件
         */
        setContentView(R.layout.activity_photo);
        surfaceView = (SurfaceView) findViewById(R.id.surfaceview);
        surfaceView.getHolder().setFixedSize(800, 480); // 设置像素
        //下面设置surfaceView不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前
        surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        surfaceView.getHolder().addCallback(new SurfaceCallback());

    }

    private class SurfaceCallback implements SurfaceHolder.Callback{

        private Camera camera;
        private boolean isPreview;
        private String tag;

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            try {
                camera = Camera.open();//打开硬件摄像头，这里导包得时候一定要注意是android.hardware.Camera
                WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);//得到窗口管理器
                Display display  = wm.getDefaultDisplay();//得到当前屏幕
                Camera.Parameters parameters = camera.getParameters();//得到摄像头的参数
//                parameters.setPreviewSize(display.getWidth(), display.getHeight());//设置预览照片的大小
                parameters.setPreviewFrameRate(3);//设置每秒3帧
                parameters.setPictureFormat(PixelFormat.JPEG);//设置照片的格式
                parameters.setJpegQuality(55);//设置照片的质量
                parameters.setPictureSize(display.getHeight(), display.getWidth());//设置照片的大小，默认是和     屏幕一样大
//                camera.setParameters(parameters);
                camera.setPreviewDisplay(surfaceView.getHolder());//通过SurfaceView显示取景画面
                camera.startPreview();//开始预览
                isPreview = true;//设置是否预览参数为真
            } catch (IOException e) {
            }
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width,
                                   int height) {
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            if(camera !=null){
                if(isPreview){//如果正在预览
                    camera.stopPreview();
                    camera.release();
                }
            }
        }

    }
}
